package com.das.mechanic_base.adapter.create;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.bean.create.MapReceiveStatusBean;
import com.das.mechanic_base.utils.X3DateUtils;

import java.util.ArrayList;
import java.util.List;

public class ServiceStatusAdapter extends RecyclerView.Adapter<ServiceStatusAdapter.ServiceStatusHolder> {

    private List<MapReceiveStatusBean.ReceiveGraphEntityBean> mList;

    private Context mContext;

    private boolean isExpand = false;

    public ServiceStatusAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void changeStatusList(List<MapReceiveStatusBean.ReceiveGraphEntityBean> mList) {
        this.mList = mList;
        this.isExpand = false;
        notifyDataSetChanged();
    }

    public void changeExpand(boolean isExpand) {
        this.isExpand = isExpand;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ServiceStatusHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ServiceStatusHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_service_status_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ServiceStatusHolder holder, int position) {
        MapReceiveStatusBean.ReceiveGraphEntityBean receiveBean = mList.get(position);

        holder.tv_top.setVisibility((position == 0) ? View.GONE : View.VISIBLE);

        holder.tv_bottom.setVisibility((position == getItemCount() - 1) ? View.GONE : View.VISIBLE);

        holder.tv_time.setText(X3DateUtils.LongDateWithTime(receiveBean.phasePositionTime));

        holder.tv_content.setText(receiveBean.descriptionAPP);

        boolean viewReportStatus = receiveBean.viewReportStatus;

        holder.iv_right.setVisibility(viewReportStatus ? View.VISIBLE : View.GONE);

        holder.itemView.setOnClickListener(view -> {
            if (!mList.get(position).viewReportStatus) {
                return;
            }
            if (mList.get(position).hasDetection) {
                // 检测
                Bundle bundle = new Bundle();
                bundle.putBoolean("isDark",true);
                bundle.putString("title","查看检测报告页");
                bundle.putString("path", X3Constent.HOME_FINISH_DETAIL_MAIN_URL + "workBaseId=" + mList.get(position).workBaseId + "&carNum=" + mList.get(position).carNum);
                X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);
            }else {
                // 施工
                Bundle bundle = new Bundle();
                bundle.putBoolean("isDark",true);
                bundle.putString("title","查看施工报告页");
                bundle.putString("path", X3Constent.HOME_FINISH_DETAIL_URL + "id=" + mList.get(position).workBaseId);
                X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH,bundle);
            }
        });

    }

    @Override
    public int getItemCount() {
        return isExpand ? mList.size() : (mList.size() >= 3 ? 3 : mList.size());
    }

    class ServiceStatusHolder extends RecyclerView.ViewHolder {
        TextView tv_time;
        TextView tv_content;
        ImageView iv_right;
        TextView tv_top;
        TextView tv_bottom;
        public ServiceStatusHolder(@NonNull View itemView) {
            super(itemView);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_content = itemView.findViewById(R.id.tv_content);
            iv_right = itemView.findViewById(R.id.iv_right);
            tv_top = itemView.findViewById(R.id.tv_top);
            tv_bottom = itemView.findViewById(R.id.tv_bottom);
        }
    }
}
